<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" circle @click="logout">退出</el-button>
    </el-header>
    <el-container class="main">
      <!-- 侧边栏 -->
      <el-aside :width="iscp ? '64px' : '200px'">
        <div class="toggle-button" @click="togglecp"> | | | </div>
        <el-menu
          router
          :collapse="iscp"
          :collapse-transition='false'
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :default-active="'/' + activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            v-for="list in menulist"
            :key="list.id"
            :index="list.id + ''"
          >
            <template slot="title">
              <i :class="iconslist[list.id]"></i>
              <span>{{list.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="item in list.children"
              :key="item.id"
              :index="'/' + item.path"
              @click="activeStatu(item.path)"
            >
              <template slot="title">
                <i class="el-icon-s-operation"></i>
                <span>{{item.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主要内容区域 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      menulist: [],
      iconslist: {
        125: 'el-icon-s-custom',
        103: 'el-icon-unlock',
        101: 'el-icon-s-shop',
        102: 'el-icon-sell',
        145: 'el-icon-s-marketing'
      },
      iscp: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    // console.log(sessionStorage.getItem('activePath'))
    this.activePath = sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    getMenuList () {
      this.$http.getMenuList()
        .then(resp => {
          const { data } = resp
          if (data.meta.status !== 200) return this.message.console.error(data.meta.msg)
          this.menulist = data.data
          // console.log(this.menulist)
        })
    },
    togglecp () {
      this.iscp = !this.iscp
    },
    activeStatu (path) {
      // console.log(path)
      this.activePath = path
      sessionStorage.setItem('activePath', path)
    }
  }
}
</script>

<style lang="scss" scoped>
  .home-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    .main {
      flex: 1;
      display: flex;
      .el-main {
        background-color: #eaedf1;
      }
      .el-aside {
        background-color: #333744;
        .el-menu {
          border: none;
        }
        .toggle-button {
          background-color: #4a5064;
          font-size: 12px;
          line-height: 24px;
          text-align: center;
          cursor: pointer;
        }
      }
    }
    .el-header {
      background-color: #373d41;
      padding: 0 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      >div {
        display: flex;
        img {
          width: 130px;
          height: 57px;
        }
        span {
          color:#fff;
          font-size: 20px;
          align-self: center;
          margin-left: 15px;
        }
      }
    }
  }
</style>
